<template>
  <div class="app-container">
    <el-form @submit.native.prevent :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px" :label-position="labelPosition">
      <el-form-item label="客户" prop="khmc">
        <el-select
            v-model="queryParams.khmc"
            placeholder="请输入客户"
            clearable
            @change="changeSelectClient"
            filterable
            :loading="selectClient3Loading"
        >
            <el-option
                v-for="(item, index) in cilentList"
                :key="index"
                :label="item.khmc"
                :value="item.khmc"
            />
        </el-select>
      </el-form-item>
      <el-form-item label="结存天数">
        <el-radio v-model="queryParams.gaoling" label="180" @change="getList">180天</el-radio>
        <el-radio v-model="queryParams.gaoling" label="365" @change="getList">365天</el-radio>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-descriptions>
      <el-descriptions-item label="总件数">{{ highAgeGoodsSummary.sumSpjs}}件</el-descriptions-item>
      <el-descriptions-item label="总重量">{{ highAgeGoodsSummary.sumSpsl}}吨</el-descriptions-item>
    </el-descriptions>
    <el-table
      v-loading="loading"
      :data="highAgeGoodsList"
    > 
      <af-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="left">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="openLeftGoods(scope.row)"
            v-hasPermi="['zhcloud:sjfx:list']"
          >剩余货物</el-button>
          <el-button
            size="mini"
            type="text"
            @click="openWarehousePositionCategory(scope.row)"
            v-hasPermi="['zhcloud:sjfx:list']"
          >按仓区分</el-button>
        </template>
      </af-table-column>
      <af-table-column prop="allName" label="客户" fixed="left"></af-table-column>
      <af-table-column prop="spjs" label="结存件数"></af-table-column>
      <af-table-column prop="spsl" label="结存重量(吨)"></af-table-column>
    </el-table>
     <pagination
        v-show="total>0"
        :total="total"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        @pagination="getList"
      />
  </div>

</template>

<script>
import { getListHighAgeGoods } from "@/api/storage/dataAnalysis/highAgeGoods";
import { selectClient3 } from "@/api/storage/customer";

export default {
  name: "highAgeGoods",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      //对齐
      labelPosition: "right",
      highAgeGoodsDate:'',
      //加载列表
      loading: true,
      //加载客户
      selectClient3Loading: true,
      // 查询参数
      queryParams: {
        khmc: "",
        gaoling: "180"
      },
      // 高龄货物列表
      highAgeGoodsList: [],
      //客户列表
      cilentList:[],
      //汇总
      highAgeGoodsSummary: {
        sumSpjs:"",
        sumSpsl:"",
      },
      //页数
      pageSize: 10,
      //页码
      pageNum:1,
      // 总条数
      total: 0,
    };
  },
  beforeRouteEnter(to, from, next) {
    const params = to.params
    next(vm => {   
      if(Object.keys(params).length > 0) {
        if(to.params.refresh == true) {
          vm.getList()
        }
      }
    })
  },
  created() {
    this.getCilent()
    this.getList()
  },
  methods: {
    /**获取客户 */
    getCilent() {
      this.selectClient3Loading = true
      selectClient3({}).then(response => {
        this.cilentList = response.data
        this.selectClient3Loading = false
      });
    },
    /** 查询高龄货物列表 */
    getList() {
      this.loading= true
      getListHighAgeGoods(this.queryParams, {pageNum:this.pageNum, pageSize:this.pageSize}).then(response => {
        const data = response.rows
        let highAgeGoodsSummary = {
          sumSpjs: 0.00,
          sumSpsl: 0.00
        }
        if(data && data.length) {
          highAgeGoodsSummary = {
            sumSpjs: data[0].sumSpjs,
            sumSpsl: data[0].sumSpsl,
          }
        }
        this.highAgeGoodsSummary  = highAgeGoodsSummary
        this.highAgeGoodsList = data
        this.total = response.total;
        this.loading= false
      });
    },

    /**客户改变 */
    changeSelectClient() {
      this.getList()
    },

    /**打开剩余货物 */
    openLeftGoods(data) {
      const gaoling = this.queryParams.gaoling
      this.$router.push({path:"/storage/dataAnalysis/highAgeGoods/highAgeLeftGoods", query:{
        gaoling:gaoling,
        khid:data.spkhmcid,
        allName:data.allName
      }})
    },

    /**打开按仓区分 */
    openWarehousePositionCategory(data) {
      const gaoling = this.queryParams.gaoling
      this.$router.push({path:"/storage/dataAnalysis/highAgeGoods/highAgeWarehouse", query:{
        gaoling:gaoling,
        khid:data.spkhmcid,
        allName:data.allName
      }})
    },
       /**enter键触发 */
    enterClick(e) {
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.reset()
      this.getCilent()
      this.getList()
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
    },
  }
};
</script>
